import { render } from "vue";
import { createVNode } from "vue";
import Confirm from "./Confirm.vue";

// 容器
const div = document.createElement("div");
// 写个标识，能认出来这个div是toast
div.setAttribute("class", "confirm-container");
// 挂载容器
document.body.appendChild(div);

//返回一个promise，无论是取消还是确认都要销毁
export default ({ title, text }) => {
  // render(虚拟节点，dom容器)
  return new Promise((res, rej) => {
    const cancelCallback = () => {
      render(null, div);
      rej(new Error("点击取消"));
    };

    const submitCallback = () => {
      render(null, div);
      res();
    };

    const vnode = createVNode(Confirm, {
      title,
      text,
      cancelCallback,
      submitCallback,
    });
    render(vnode, div);
  });
};
